package com.cvs.contorller;

import com.cvs.bean.JiFen;
import com.cvs.bean.JiFenGood;
import com.cvs.bean.Orders;
import com.cvs.bean.User;
import com.cvs.service.JiFenGoodService;
import com.cvs.service.JiFenService;
import com.cvs.service.OrdersService;
import com.cvs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jifen")
public class JiFenGoodController {
    @Autowired
    private UserService userService;
    @Autowired
    private JiFenGoodService jiFenGoodService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private JiFenService jiFenService;
    @RequestMapping("/index")
    public String jifenGood(int uid,Model model,int pageNum,int pageSize){
        User user=userService.findOne(uid);
        model.addAttribute("user",user);//用户信息

        //可以兑换的产品列表
        //List<JiFenGood> list=jiFenGoodService.findAll();
        model.addAttribute("goods",jiFenGoodService.findAllByPage(pageNum,pageSize));//
        return "jifenGood";
    }
    @RequestMapping("/jiFenGoods")
    public @ResponseBody Map jiFenGoods(int uid,int pageNum,int pageSize){

        Map map =new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",jiFenGoodService.findAllByPage(pageNum,pageSize));
        return map;
    }
    @RequestMapping("/jifenGoodInfo")
    public String jifenGoodInfo(int uid,int id,Model model){
        User user=userService.findOne(uid);
        model.addAttribute("user",user);//用户信息

        model.addAttribute("goodInfo",jiFenGoodService.findOne(id));//
        return "jifenGoodInfo";
    }
    @RequestMapping("/jifenBuy")
    public String jifenBuy(int uid,int id,Model model){
        User user=userService.findUserInfo(uid);
        System.out.println(user);
        model.addAttribute("user",user);//用户信息
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 =new SimpleDateFormat("yyyyMMddHHmmss");
        model.addAttribute("createtime",simpleDateFormat.format(new Date()));//下单时间
        model.addAttribute("orderNum",simpleDateFormat1.format(new Date()));//下单时间
        model.addAttribute("goodInfo",jiFenGoodService.findGoodInfo(id));//
        return "jifenBuy";
    }
    @RequestMapping("/makeOrder")
    public String makeOrder(int uid,int id,String orderNo,Model model){
        User user=userService.findOne(uid);
        JiFenGood jiFenGood=jiFenGoodService.findOne(id);
        if(user.getJifen()<jiFenGood.getMoney()){
            return "fail";
        }
        model.addAttribute("user",user);//用户信息
        //构建orders信息
        Orders orders=new Orders();
        orders.setUid(uid);
        orders.setGid(id);
        orders.setCount(1);
        orders.setOrderno(orderNo);
        orders.setOrdertime(new Date());
        orders.setStatues(1);
        //下单支付
        int flg=ordersService.insertOrder(orders);

        //扣除个人积分
        //购买后剩余积分

        int leftjf=user.getJifen()-jiFenGood.getMoney();
        user.setJifen(leftjf);
        userService.updateUser(user);//更新用户信息

        //增加积分记录
        JiFen jiFen =new JiFen();
        jiFen.setCreatetime(new Date());
        jiFen.setUid(uid);
        jiFen.setMoney(jiFenGood.getMoney());//设置兑换消耗的金额
        //jiFen.setActivity();
        jiFenService.addJiFenLog(jiFen);

        //减少可兑换商品数量
        jiFenGood.setUsecount(jiFenGood.getUsecount()+1);
        jiFenGoodService.updateJiFenGood(jiFenGood);
        model.addAttribute("goodInfo",jiFenGood);
        model.addAttribute("orders",orders);
        return "orders";
    }
}
